package com.usian.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.entity.*;
import com.usian.service.ComService;
import com.usian.service.FenginEmpService;
import com.usian.service.FenginTeamsService;
import com.usian.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈宇轩
 * @version 1.0
 * @date 2023/8/23 21:13
 */

@RequestMapping("/com")
@RestController
public class ComController {
    @Autowired
    private ComService comService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FenginEmpService fenginEmpService;
    @Autowired
    private FenginTeamsService fenginTeamsService;
    @GetMapping("/findAllTeamsList")
    public List<Teams> findAllTeamsList(){
        return fenginTeamsService.findAllTeamsList();
    }
    @GetMapping("/findAllCom")
    public ResultResponse findAllCom(){
        try {
            Integer ss=comService.findAllCom();
            return ResultResponse.Success(ss);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("/findList")
    public List<Emp> findList(){
        return fenginEmpService.findList();
    }
    @PostMapping("/add")
    public ResultResponse add(@RequestBody Com com){
        try {
            Integer id = (Integer) redisTemplate.opsForValue().get("竞赛ID");
            com.setId(id);
            com.setStatus(2);
            comService.updateById(com);
            return ResultResponse.Success("ok");
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("/findAll")
    public ResultResponse findAll(ComDTO comDTO){
        try {
            PageHelper.startPage(comDTO.getPageNum(),comDTO.getPageSize());
            List<Com> list=comService.findAll(comDTO);
            PageInfo<Com> pageInfo = new PageInfo<>(list);
            return ResultResponse.Success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("/setRedisComId")
    public ResultResponse setRedisComId(Integer comId){
        redisTemplate.opsForValue().set("竞赛ID",comId);
        return ResultResponse.Success("111111");
    }

    @GetMapping("/findCom")
    public ResultResponse findCom(){
        Integer comID = (Integer) redisTemplate.opsForValue().get("竞赛ID");
        Com com = comService.getById(comID);
        return ResultResponse.Success(com);
    }
}
